import { defineStore } from 'pinia'
import { ref } from 'vue'
// import { Toast } from 'vant';
import { showToast } from 'vant';
import { useRouter } from 'vue-router';
import { getUserInfo } from "../api/userInfoView"

export const useCounterStore = defineStore('counter', () => {
    let router = useRouter()
    let getItems: any = ref(window.localStorage.getItem("idarr"))
    const idarr = ref(JSON.parse(getItems.value) || [])
    const tocked = ref(window.localStorage.getItem("token"))
    const userid: any = ref(window.localStorage.getItem("userId"))

    // 关注
    const increment = (id?: number) => {
        if (tocked.value) {
            idarr.value.push(id)
            window.localStorage.setItem("idarr", JSON.stringify(idarr.value));
            console.log(id);
        } else {

            showToast('请登录');
            router.push({
                path: '/register',
            });


        }



    }
    // 取消关注
    const incrementfalse = (id?: number) => {
        let attebtionarr: any = window.localStorage.getItem('idarr');
        idarr.value = JSON.parse(attebtionarr) || [];
        idarr.value = idarr.value.filter((item: any) => item != id);
        window.localStorage.setItem("idarr", JSON.stringify(idarr.value));



    }


    let getlocalid: any = ref(window.localStorage.getItem("likearr"))
    const likearr: any = ref(JSON.parse(getlocalid.value) || [])

    // 点击喜欢
    const likeFuns = (id?: number) => {


        if (tocked.value) {
            likearr.value.push(id)
            window.localStorage.setItem("likearr", JSON.stringify(likearr.value));
            console.log(id)
        } else {

            showToast('请登录');
            router.push({
                path: '/register',
            });


        }

        ;

    }
    // 取消喜欢
    const unlikeFuns = (id?: number) => {
        let attebtionarr: any = window.localStorage.getItem('likearr');
        likearr.value = JSON.parse(attebtionarr) || [];
        likearr.value = likearr.value.filter((item: any) => item != id);
        window.localStorage.setItem("likearr", JSON.stringify(likearr.value));
    }



    // 评论点赞
    let getcommentid: any = ref(window.localStorage.getItem("commentlikearr"))
    const commentlikearr: any = ref(JSON.parse(getcommentid.value) || [])
    // 点击评论喜欢
    const commentFuns = (id?: number) => {
        if (tocked.value) {
            commentlikearr.value.push(id)
            window.localStorage.setItem("commentlikearr", JSON.stringify(commentlikearr.value));
        } else {

            showToast('请登录');
            router.push({
                path: '/register',
            });


        }



    }
    // 取消评论喜欢
    const uncommentFuns = (id?: number) => {

        commentlikearr.value = commentlikearr.value.filter((item: any) => item != id);
        window.localStorage.setItem("commentlikearr", JSON.stringify(commentlikearr.value));
    }


    // 收藏

    let getcollectid: any = ref(window.localStorage.getItem("collectlikearr"))
    const collectlikearr: any = ref(JSON.parse(getcollectid.value) || [])
    // 点击收藏
    const collectFuns = (id?: number) => {
        if (tocked.value) {
            collectlikearr.value.push(id)
            window.localStorage.setItem("collectlikearr", JSON.stringify(collectlikearr.value));

        } else {

            showToast('请登录');
            router.push({
                path: '/register',
            });


        }


    }
    // 取消收藏
    const uncollectFuns = (id?: number) => {

        collectlikearr.value = collectlikearr.value.filter((item: any) => item != id);
        console.log('8888889 collectlikearr.value', collectlikearr.value)

        window.localStorage.setItem("collectlikearr", JSON.stringify(collectlikearr.value));
        // let attebtionarr: any = window.localStorage.getItem('collectlikearr');
        // collectlikearr.value = JSON.parse(attebtionarr) || [];
        console.log('888888 collectlikearr.value', collectlikearr.value)
    }


    // 获取登录以后头像名字用于发布
    // 用户信息,头像,nickname
    const userInfoheard: any = ref([])
    const getUserInfoFun = (): any => {
        getUserInfo(userid.value).then((data: any) => {
            console.log("getUserInfo1111111111111=>", data)
            userInfoheard.value = data.data;
            console.log("pinia22222222222222=>", userInfoheard.value);
        })
    }


    const num: any = ref(0)
    const changnum = (length: any) => {
        num.value = length
        console.log("num的值", length);


    }
    //登陆成功函数
    const successLogin = () => {
        tocked.value = window.localStorage.getItem("token")
        userid.value = window.localStorage.getItem("userId");
        console.log('successLogin执行了')
    }
    const backpath: any = ref(null);
    // const changepath:any = (backpath:any) => {
    //     path.value = backpath;
    // }
    let changepath = (topath: any) => {
        console.log('执行了store中的函数', backpath.value)
        backpath.value = topath;
    }

    return { backpath, changepath, idarr, likearr, commentlikearr, collectlikearr, num, successLogin, userid, tocked, userInfoheard, changnum, increment, incrementfalse, likeFuns, unlikeFuns, uncommentFuns, commentFuns, collectFuns, uncollectFuns, getUserInfoFun }
})